package com.chushouya.controller.manager.api;

import com.chushouya.common.annotations.AuthSkip;
import com.chushouya.common.constant.ServiceType;
import com.chushouya.common.support.Contexts;
import com.chushouya.manager.constants.SmsSceneEnum;
import com.chushouya.manager.dto.admin.user.login.*;
import com.chushouya.manager.dto.api.sms.SendCodeDTO;
import com.chushouya.manager.service.api.UserLoginService;
import com.general.framework.core.schema.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * c端用户登录接口
 */
@RestController
@RequestMapping(ServiceType.API + "/login")
@Api(tags = "登录接口")
public class UserLoginApiController {

    @Resource
    private UserLoginService userLoginService;


    @PostMapping("/appletLogin")
    @ApiOperation("小程序登录")
    @AuthSkip
    public Response<UserInfoDTO> appletLogin(@RequestBody @Valid AppletLoginDTO dto) {
        return Response.success(userLoginService.appletLogin(dto));
    }

    @PostMapping("/clerkLogin")
    @ApiOperation("业务员登录")
    @AuthSkip
    public Response<UserInfoDTO> clerkLogin(@RequestBody @Valid ClerkLoginDTO dto) {
        return Response.success(userLoginService.clerkLogin(dto));
    }

    @PostMapping("/clerkSmsLogin")
    @ApiOperation("业务员短信登录")
    @AuthSkip
    public Response<UserInfoDTO> clerkSmsLogin(@RequestBody @Valid SmsLoginDTO dto) {
        return Response.success(userLoginService.clerkSmsLogin(dto));
    }

    @PostMapping("/getPhone")
    @ApiOperation("获取小程序手机号")
    @AuthSkip
    public Response<String> getPhone(@RequestBody @Valid AppletLoginDTO dto) {
        return Response.success(userLoginService.getAppletUserPhone(dto));
    }

    @PostMapping("/smsCode")
    @ApiOperation("发送登录验证码")
    @AuthSkip
    public Response<Void> smsCode(@RequestBody @Valid SendCodeDTO dto) {
        String scene = SmsSceneEnum.H5_LOGIN.value();
        final String phone = dto.getPhone();
        userLoginService.sendCode(scene, phone);
        return Response.success();
    }

    @PostMapping("/smsLogin")
    @ApiOperation("验证码登录")
    @AuthSkip
    public Response<UserInfoDTO> smsLogin(@RequestBody @Valid SmsLoginDTO dto) {
        return Response.success(userLoginService.smsLogin(dto));
    }

    @PostMapping("/code2Session")
    @ApiOperation("code换取用户id")
    @AuthSkip
    public Response<String> code2Session(@RequestBody @Valid Code2SessionDTO dto) {
        return Response.success(userLoginService.code2Session(dto));
    }
    @PostMapping("/code2AlipayOpenId")
    @ApiOperation("支付宝code补足用户信息")
    public Response<Void> code2OpenId(@RequestBody @Valid Code2SessionDTO dto) {
        userLoginService.code2AlipayOpenId(dto);
        return Response.success();
    }

    @PostMapping("jPushLogin")
    @ApiOperation("极光一键登录")
    @AuthSkip
    public Response<UserInfoDTO> jPushLogin(@RequestBody @Valid JPushLoginDTO dto) {
        return Response.success(userLoginService.jPushLogin(dto));
    }

    @GetMapping("/userInfo")
    @ApiOperation("登录用户信息")
    public Response<UserInfoDTO> userInfo(String appId) {
        return Response.success(userLoginService.getLoginUserInfo(appId, Contexts.getUserContext().getUserId()));
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Response<Void> logout() {
        userLoginService.logout(Contexts.getHeaderContext().getToken());
        return Response.success();
    }

    @PostMapping("/passwordLogin")
    @ApiOperation("密码登录")
    @AuthSkip
    public Response<UserInfoDTO> passwordLogin(@RequestBody @Valid PasswordLoginDTO dto) {
        return Response.success(userLoginService.passwordLogin(dto));
    }

}
